var previousPickedEntity = undefined;
var previousPickedArea = undefined;
var handler_mouseMove_01;
var handler_mouseMove_02;
var handler_mouseMove_03;

var handler_mouseMove = {
    /*鼠标经选效果（从全景选片区）*/
    mouseMove_01: function(){
        handler_mouseMove_01 = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        handler_mouseMove_01.setInputAction(
            (movement)=>{
                var pickedPrimitive = viewer.scene.pick(movement.endPosition);
                var pickedEntity = (Cesium.defined(pickedPrimitive)) ? pickedPrimitive.id : undefined;
                var layer = scene.layers.find("片区划分_150m");

                
                if(Cesium.defined(previousPickedEntity)){
                    previousPickedEntity.billboard.scale = 1.0;                                          //Unhighlight the previously picked entity
                    layer.setOnlyObjsVisible([1,2,3,4,5], false);
                }
                if (Cesium.defined(pickedEntity) && Cesium.defined(pickedEntity.billboard)){
                    pickedEntity.billboard.scale = 1.2;                                                  //Highlight the current picked entity
                    previousPickedEntity = pickedEntity;
                    var polygonId = pickedEntity.id;
                    var boundaryArr = [
                        {name:"board_BAZX", value: 4},
                        {name:"board_DCW", value: 5},
                        {name:"board_GW", value: 2},
                        {name:"board_QW", value: 1},
                        {name:"board_MW", value: 3},
                    ];
                    var boundaryObj = boundaryArr.find(item=>{
                        return item.name == polygonId
                    })
                    layer.setObjsVisible([boundaryObj.value], true);
                };
            }
        ,Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    },
    /*鼠标经选效果（从片区选单元）*/
    mouseMove_02:function(){
        handler_mouseMove_02 = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        handler_mouseMove_02.setInputAction(
            (movement)=>{
                var pickedPrimitive = viewer.scene.pick(movement.endPosition);
                var pickedEntity = (Cesium.defined(pickedPrimitive)) ? pickedPrimitive.id : undefined;
                var layer = scene.layers.find("version3");
                
               
                if(Cesium.defined(previousPickedEntity)){
                    previousPickedEntity.billboard.scale = 1.0;                                             //Unhighlight the previously picked entity
                    layer.setOnlyObjsVisible([], true);
                }
                
                if (Cesium.defined(pickedEntity) && Cesium.defined(pickedEntity.billboard)){
                    pickedEntity.billboard.scale = 1.2;                                                     //Highlight the current picked entity
                    previousPickedEntity = pickedEntity;
                    var polygonId = parseInt(pickedEntity.id);                                              //字转数
                    layer.setObjsVisible([polygonId],true);
                    
                };
            }
        ,Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    },
    /*鼠标经选效果（从单元选建筑）*/
    mouseMove_03:function(){
        handler_mouseMove_03 = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        handler_mouseMove_03.setInputAction(
            (movement)=>{
                var pickedPrimitive = viewer.scene.pick(movement.endPosition);
                var pickedEntity = (Cesium.defined(pickedPrimitive)) ? pickedPrimitive.id : undefined;
                
                if(Cesium.defined(previousPickedEntity)){
                    previousPickedEntity.billboard.scale = 1.0;                                             //Unhighlight the previously picked entity
                    
                    //退出billboard后弹窗关闭
                    $(".information").css("visibility","hidden");

                    //恢复图层颜色
                    selected_buildings.forEach((item)=>{item.style3D.fillForeColor=new Cesium.Color(1.0, 1.0, 1.0)})
                }
                
                if (Cesium.defined(pickedEntity) && Cesium.defined(pickedEntity.billboard)){
                    pickedEntity.billboard.scale = 1.2;                                                     //Highlight the current picked entity
                    
                    //改变选择图层颜色
                    var layerName = pickedEntity.name;

                    scene.layers.find(layerName).style3D.fillForeColor=new Cesium.Color(0,216/255,1);
                    
                    //经停标签加入信息泡泡
                    var building_link ="";
                    var forehead = "http://qhbim.qhholding.com:8001/qhszpm/planb/pages/index.html?_pkValue=";
                    var backside = "&sysprjid=";
                        //找出匹配图层名称jhmxname的pkValue组成building_link
                    var building_pkValue;
                    var highlight_building;
                    building_pkValues.forEach((item)=>{
                        if(layerName==item.jhmxname){
                            highlight_building = item
                            building_pkValue = item.pkValue
                        } 
                    });    
                    building_link = forehead+building_pkValue+backside;
                        //显示模型信息
                    if(building_pkValue){
                        
                        console.log(building_link);
                        //在鼠标hover位置打开弹窗
                        $(".information").empty();
                        $(".information").css("visibility","visible");
                        $(".information").css("left",movement.endPosition.x+20);
                        $(".information").css("top",movement.endPosition.y);
                        $(".information").append('<h3>'+highlight_building.name+'</h3><p>房建模型：有</p>')
                    }
                        else{
                        
                        //在鼠标hover位置打开弹窗
                        $(".information").empty();
                        $(".information").css("visibility","visible");
                        $(".information").css("left",movement.endPosition.x+20);
                        $(".information").css("top",movement.endPosition.y);
                        // $(".information").append('<h3>'+highlight_building.name+'</h3><p>房建模型：无</p>');
                        
                        //直接显示无pkValues图层信息
                        $(".information").append('<h3>'+layerName.split("@")[0]+'</h3><p>房建模型：无</p>');
                    };


                    previousPickedEntity = pickedEntity;
                };
            }
        ,Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    }
}


handler_mouseMove.mouseMove_01();                                         //状态初始化